﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Brewmaster
{
    public partial class TabMenu : UserControl
    {

        private const int buttonHeight = 50;
        private const int padding = 5;

        private List<TabMenuPage> tabCollection;
        private List<Panel> panelCollection;

        public TabMenu()
        {
            InitializeComponent();
            tabCollection = new List<TabMenuPage>();
            panelCollection = new List<Panel>();
            this.Resize += new EventHandler(TabMenu_Resize);
        }

        void TabMenu_Resize(object sender, EventArgs e)
        {
            resizeControls();
        }

        [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
        public List<TabMenuPage> Tabs
        {
            get
            {
                return tabCollection;
            }
            set
            {
                foreach (TabMenuPage tab in tabCollection)
                {
                    tab.menuRadioButton.radioButton.CheckedChanged += new EventHandler(radioButton_CheckedChanged);
                }
                this.Controls.Clear();
                int limit = panelCollection.Count;
                for (int i = 0; i < limit; i++) // get rid of the unused panels
                {
                    Panel panel = panelCollection.ElementAt(i);
                    bool erase = true;
                    foreach (TabMenuPage tab in value)
                    {
                        if (tab.panel == panel)
                        {
                            erase = false;
                            break;
                        }
                    }
                    if (erase)
                    {
                        panelCollection.Remove(panel);
                        limit = panelCollection.Count;
                    }
                }
                foreach (TabMenuPage tab in value) // create panels for new tab pages
                {
                    if (tab.panel == null)
                    {
                        tab.panel = new Panel ();
                        panelCollection.Add(tab.panel);
                    }
                }
                foreach (Panel panel in panelCollection)
                {
                    panel.Visible = false;
                }
                tabCollection = value;
                populateMe();
            }
        }

        void radioButton_CheckedChanged(object sender, EventArgs e)
        {
            foreach (TabMenuPage tab in tabCollection)
            {
                if (tab.menuRadioButton.Checked)
                {
                    tab.panel.Visible = true;
                }
                else
                {
                    tab.panel.Visible = false;
                }
            }
        }

        private void populateMe()
        {
            foreach (TabMenuPage tab in tabCollection)
            {
                this.Controls.Add(tab.menuRadioButton);
                this.Controls.Add(tab.panel);
            }
            resizeControls();
            tabCollection.ElementAt(0).menuRadioButton.Checked = true; // select the first tab
        }

        private void resizeControls()
        {
            int tabCount = tabCollection.Count;
            if (tabCount > 0)
            {
                int buttonWidth = (this.Width - (tabCount + 1) * padding) / tabCount;
                int panelHeight = this.Height - buttonHeight - 2 * padding;
                for (int i = 0; i < tabCount; i++)
                {
                    MenuRadioButton currentButton = tabCollection.ElementAt(i).menuRadioButton;
                    currentButton.Top = padding;
                    currentButton.Left = i * buttonWidth + (i + 1) * padding;
                    currentButton.Height = buttonHeight;
                    currentButton.Width = buttonWidth;
                }
                foreach (Panel panel in panelCollection)
                {
                    panel.Dock = DockStyle.Bottom;
                    panel.Height = panelHeight;
                }
            }
        }
    }
}
